//
//  ViewController.m
//  电话薄
//
//  Created by 123 on 17/9/7.
//  Copyright © 2017年 苹果1512. All rights reserved.
//
#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
    UITableView * tableview;
    NSMutableArray * mutable;
    NSArray * backup;
    NSMutableArray * tel;
    NSMutableArray * opentel;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    tel = [NSMutableArray new];
    opentel = [NSMutableArray new];
    mutable = [NSMutableArray new];
    [mutable addObject:@"张,345459"];
    [mutable addObject:@"李,346798"];
    [mutable addObject:@"王,123456"];
    backup = mutable.copy;
    UISearchBar * bar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    self.navigationItem.titleView = bar;
    
    
    
    bar.delegate =self;
    tableview.delegate = self;
    tableview.dataSource = self;
    
    
    
    [self.view addSubview:tableview];
    
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //NSLog(@"%@",searchText);
    if(searchText.length == 0)
    {
        mutable = backup.copy;
        [tableview reloadData];
        return ;
    }
    
    NSMutableArray * res = [NSMutableArray new];
    for(NSString * str in backup)
    {
        int i=0;
        if([str containsString:searchText])
        {
            [res addObject:str];
            if(i%2)
            {
                [tel addObject:str];
            }
        }
    }
    mutable = res;
    //NSLog(@"%@",mutable);
    [tableview reloadData];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return mutable.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableview dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    NSString * str = mutable[indexPath.row];
    NSArray * arr = [str componentsSeparatedByString:@","];
    
    
    cell.textLabel.text = arr.firstObject;
    cell.detailTextLabel.text = arr.lastObject;
    [opentel addObject:arr.lastObject];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击");
    //NSLog(@"%@",opentel[indexPath.row]);
    NSString *str = opentel[indexPath.row];
    //拨打电话
    //[[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",str]]];
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"拨打电话" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"%@",opentel[indexPath.row]);
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",str]]];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"发送邮件" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }]];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end